using System;
using System.Collections;
using UnityEngine;

public class RagdollReplace : MonoBehaviour
{
	public float DisableDelay = 5f;

	public GameObject RootRagdoll;

	private float timeTemp;

	private void Start()
	{
		timeTemp = Time.time;
		if (RootRagdoll == null)
		{
			FindRoot(base.gameObject);
		}
	}

	private void FindRoot(GameObject dst)
	{
		IEnumerator enumerator = dst.transform.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				Transform transform = (Transform)enumerator.Current;
				if ((bool)transform.GetComponent<Rigidbody>())
				{
					RootRagdoll = transform.gameObject;
					break;
				}
				FindRoot(transform.gameObject);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	private void FindRigidBody(GameObject dst)
	{
		if ((bool)dst.GetComponent<Rigidbody>())
		{
			dst.GetComponent<Rigidbody>().isKinematic = true;
		}
		if ((bool)dst.GetComponent<Collider>())
		{
			UnityEngine.Object.Destroy(dst.GetComponent<Collider>());
		}
		IEnumerator enumerator = dst.transform.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				Transform transform = (Transform)enumerator.Current;
				FindRigidBody(transform.gameObject);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	private void Update()
	{
		if (Time.time >= timeTemp + DisableDelay && (RootRagdoll == null || ((bool)RootRagdoll && RootRagdoll.GetComponent<Rigidbody>().velocity.sqrMagnitude <= 0.01f)))
		{
			FindRigidBody(base.gameObject);
			UnityEngine.Object.Destroy(this);
		}
	}
}
